package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.OutUrlConstant;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.IYXGAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author sdq
 * @Date 2022/7/5 9:49
 */
@Slf4j
@Service
public class YXGAPIImpl implements IYXGAPI {
    private static final String YXG_SECRET_KEY = "YGRGERUEIREJEKFJE";
    @Resource
    private InfoImgMapper infoImgMapper;
    @Autowired
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;

    private static String YXGURL = null;


    //拼装参数
    private Map<String,Object> params(AccountInfo accountInfo,SalePartner sa){
//        if(!Arrays.asList("王者荣耀","和平精英","英雄联盟").contains(accountInfo.getType())){
//            throw new GlobalException("游戏阁接口不支持此游戏：" + accountInfo.getType());
//        }
        this.YXGURL = sa.getOther();
        Map<String,Object> params = new HashMap<>();
        Map<String,Object> product = new HashMap<>();

        product.put("id",accountInfo.getId().intValue());
        product.put("title","【"+descTitle + "_" + accountInfo.getId() + "】  "+ accountInfo.getLevel() + "  " + accountInfo.getDes());
        product.put("daqu",accountInfo.getRegional());
        if(StringUtils.isBlank(accountInfo.getTitleImg())){
            product.put("pic",sa.getTwoOther());
        }else{
            product.put("pic",accountInfo.getTitleImg());
            //详情
            StringBuilder imagesSb = new StringBuilder();
            InfoImg infoImg = new InfoImg();
            infoImg.setInfoId(accountInfo.getId());
            infoImg.setType(Constants.STR_1);
            List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);

            if(!CollectionUtils.isEmpty(infoImgs)){
                for (InfoImg img : infoImgs) {
                    imagesSb.append(img.getImgUrl()).append(",");
                }
                String images = imagesSb.toString();
                product.put("pic",accountInfo.getTitleImg()+","+images);
            }
        }
        BigDecimal bigDecimal = commonApi.addPrice(accountInfo.getPrice(), sa.getSeleep());
        commonApi.setPrice(bigDecimal,accountInfo.getId(),sa);
        product.put("price",bigDecimal.toString());
        if("王者荣耀".equals(accountInfo.getType())){
            product.put("pf",accountInfo.getSkins().toString());
            product.put("gameid",3);
        }
        if("和平精英".equals(accountInfo.getType())){
            product.put("gameid",5);
        }
        if("英雄联盟".equals(accountInfo.getType())){
            product.put("gameid",8);
        }
        if("CF端游".equals(accountInfo.getType())){
            product.put("gameid",1);

            if (accountInfo.getDes().contains("上海")){
                product.put("daqu","上海");
            }
            if (accountInfo.getDes().contains("江苏")){
                product.put("daqu","江苏");
            }
            if (accountInfo.getDes().contains("浙江")){
                product.put("daqu","浙江");
            }
            if (accountInfo.getDes().contains("上海")){
                product.put("daqu","上海");
            }
            if (accountInfo.getDes().contains("福建")){
                product.put("daqu","福建");
            }
            if (accountInfo.getDes().contains("安徽")){
                product.put("daqu","安徽");
            }
            if (accountInfo.getDes().contains("四川")){
                product.put("daqu","四川");
            }
            if (accountInfo.getDes().contains("重庆")){
                product.put("daqu","重庆");
            }
            if (accountInfo.getDes().contains("陕西")){
                product.put("daqu","陕西");
            }
            if (accountInfo.getDes().contains("上海")){
                product.put("daqu","上海");
            }
            if (accountInfo.getDes().contains("云南")){
                product.put("daqu","云南");
            }
            if (accountInfo.getDes().contains("山东")){
                product.put("daqu","山东");
            }
            if (accountInfo.getDes().contains("河北")){
                product.put("daqu","河北");
            }
            if (accountInfo.getDes().contains("北京")){
                product.put("daqu","北京");
            }
            if (accountInfo.getDes().contains("辽宁")){
                product.put("daqu","辽宁");
            }
            if (accountInfo.getDes().contains("河南")){
                product.put("daqu","河南");
            }
            if (accountInfo.getDes().contains("华北大区")){
                product.put("daqu","华北大区");
            }
            if (accountInfo.getDes().contains("吉林")){
                product.put("daqu","吉林");
            }
            if (accountInfo.getDes().contains("山西")){
                product.put("daqu","山西");
            }
            if (accountInfo.getDes().contains("黑龙江")){
                product.put("daqu","黑龙江");
            }
            if (accountInfo.getDes().contains("北方")){
                product.put("daqu","北方");
            }
            if (accountInfo.getDes().contains("广东")){
                product.put("daqu","广东");
            }
            if (accountInfo.getDes().contains("广西")){
                product.put("daqu","广西");
            }
            if (accountInfo.getDes().contains("湖南")){
                product.put("daqu","湖南");
            }
            if (accountInfo.getDes().contains("湖北")){
                product.put("daqu","湖北");
            }
            if (accountInfo.getDes().contains("江西")){
                product.put("daqu","江西");
            }
        }
        if("原神".equals(accountInfo.getType())){
            product.put("gameid",6);
        }

        if (Arrays.asList(LCConstant.haveTwoList).contains(accountInfo.getLevel())){
            product.put("condition",1);
        }else if (Objects.equals(LCConstant.bakFace,accountInfo.getLevel())){
            product.put("condition",2);
        }

        //cf专有字段
        product.put("paiwei","2");
        params.put("product",product);
        //签名
        String str = product.get("daqu")+product.get("gameid").toString()+product.get("id").toString()+sa.getSaleKey();
        String sign = DigestUtils.md5Hex(str);
        params.put("sign",sign);

        return params;
    }


    @Override
    public AjaxResult addGame(AccountInfo accountInfo,Long userId,SalePartner salePartner) {
        try{
            Map<String, Object> result = new HashMap<>();
            Map<String, Object> params = params(accountInfo,salePartner);
            params.put("act","add");
            String paramsStr = JSONObject.toJSONString(params);
            log.info("游戏阁新增请求参数={}",paramsStr);
            String response = "";
            if (Objects.isNull(YXGURL)){
                 response = HttpUtils.sendPost(OutUrlConstant.YXGURL, paramsStr, "application/json");
            }else {
                 response = HttpUtils.sendPost(YXGURL, paramsStr, "application/json");
            }

            log.info("游戏阁新增返回参数={}",response);
            JSONObject resultJson = JSONObject.parseObject(response);
            String msg = resultJson.getString("msg");
            msg = MapUntils.unicodeToString(msg);
            if (!resultJson.getBoolean("succ")){
                result.put("code",500);
            }else {
                result.put("code",200);
            }
            result.put("req",JSONObject.toJSONString(params));
            result.put("res",response);
            result.put("msg",msg);
            return AjaxResult.success(result);
        }catch (Exception e){
            throw new GlobalException(e.getMessage());
        }

    }

    @Override
    public AjaxResult editGame(AccountInfo accountInfo, Long userId, SalePartner salePartner) {
        try{
            Map<String, Object> params = params(accountInfo,salePartner);
            params.put("act","mod");
            String paramsStr = JSONObject.toJSONString(params);
            log.info("游戏阁编辑请求参数={}",paramsStr);
            String response = "";
            if (Objects.isNull(YXGURL)){
                response = HttpUtils.sendPost(OutUrlConstant.YXGURL, paramsStr, "application/json");
            }else {
                response = HttpUtils.sendPost(YXGURL, paramsStr, "application/json");
            }
            log.info("游戏阁编辑返回参数={}",response);
            JSONObject resultJson = JSONObject.parseObject(response);
            String msg = resultJson.getString("msg");
            msg = MapUntils.unicodeToString(msg);
            Map<String, Object> result = new HashMap<>();
            if (!resultJson.getBoolean("succ")){
                result.put("code",500);
            }else {
                result.put("code",200);
            }
            result.put("req",JSONObject.toJSONString(params));
            result.put("res",response);
            result.put("msg",msg);
            return AjaxResult.success(result);
        }catch (Exception e){
            throw new GlobalException(e.getMessage());
        }
    }

    @Override
    public AjaxResult delGame(AccountInfo accountInfo,Long userId,SalePartner salePartner) {
        Map<String, Object> params = params(accountInfo,salePartner);
        params.put("act","del");
        String paramsStr = JSONObject.toJSONString(params);
        log.info("游戏阁删除请求参数={}",paramsStr);
        String response = "";
        if (Objects.isNull(YXGURL)){
            response = HttpUtils.sendPost(OutUrlConstant.YXGURL, paramsStr, "application/json");
        }else {
            response = HttpUtils.sendPost(YXGURL, paramsStr, "application/json");
        }
        log.info("游戏阁删除返回参数={}",response);
        JSONObject resultJson = JSONObject.parseObject(response);
        String msg = resultJson.getString("msg");
        msg = MapUntils.unicodeToString(msg);
        Map<String, Object> result = new HashMap<>();
        if (!resultJson.getBoolean("succ")){
            result.put("code",500);
        }else {
            result.put("code",200);
        }
        result.put("req",JSONObject.toJSONString(params));
        result.put("res",response);
        result.put("msg",msg);
        return AjaxResult.success(result);
    }

    public static void main(String[] args) {
        Map<String,Object> params = new HashMap<>();
        //add mod del
//        params.put("act","add");
//        params.put("act","mod");
        params.put("act","del");
        Map<String,Object> product = new HashMap<>();
        //3--王者荣耀  5--和平精英
        product.put("gameid",5);
        //游戏阁商品编号，每个商品在游戏阁对应唯一的编号， 建议接收方保存此字段，商品的实时修改或者删除下架，都是根据这个字段进行逻辑处理的
        product.put("id",353);
        //
        String descTitle = "YD";
        product.put("title","【"+descTitle + "_" + "353" + "】  "+ "王者" + "  " + "女武神 胡桃夹子 太华 天狼绘梦者 暗影特工 大圣娶亲 西部牛仔");

        product.put("daqu","安卓QQ");

        product.put("pic","http://www.a.com/测试");

        product.put("price","59989");
        //皮肤数  王者荣耀专有字段
        product.put("pf","6");
        //CF专有 其他为空
        product.put("paiwei","");
        //
        params.put("product",product);

        String str = product.get("daqu")+product.get("gameid").toString()+product.get("id").toString()+"YGRGERUEIREJEKFJE";
        String sign = DigestUtils.md5Hex(str);
        params.put("sign",sign);
        System.out.println(sign);

        System.out.println(JSONObject.toJSONString(params));

        String baseUrl = "http://api.youxige.com:50001/tools/pushProductByV.ashx";
        String response = HttpUtils.sendPost(baseUrl, JSONObject.toJSONString(params), "application/json");
        System.out.println(response);
        long time = DateUtils.getNowDate().getTime();
        System.out.println(time);

    }
}
